package greaderbook.frontend;

import greaderbook.backend.GreaderBookDB;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/**
 * The main activity launcher of GreaderBook.
 * 
 * @author Jia Cherng Ho
 * 
 */
public class GreaderBook extends Activity {
	static final int RETRIEVE_SID = 0;
	private TextView lbl;
	private GreaderBookDB db;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		lbl = (TextView) findViewById(R.id.result);
		//db = new GreaderBookDB(this);
		startActivityForResult(new Intent(this, AuthenticationView.class),
				RETRIEVE_SID);
	}

	/**
	 * Save user data returned from AuthenticationView into database.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Bundle result = data.getExtras();
		if (requestCode == RETRIEVE_SID && resultCode == RESULT_OK
				&& data.hasExtra("user") && data.hasExtra("pass")
				&& data.hasExtra("sid")) {
			lbl.setText("User: " + result.getString("user") + ", Pass: "
					+ result.getString("pass") + ", SID: "
					+ result.getString("sid"));
			// db.insert(result.getString("user"), result.getString("pass"),
			// result.getString("sid"));
		} else {
			lbl.setText("Authentication failed.");
		}
	}
}